package com.ruoyi.service.achievements.impl;

import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.mapper.achievements.AcvTemplateManageMapper;
import com.ruoyi.service.achievements.AcvTemplateManageService;
import com.ruoyi.util.StringUtil;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

/**
 * @author zeo
 * @explain 绩效模板服务实现类
 * @date 2020/8/22 10:48
 */
@Service
public class AcvTemplateManageServiceImpl implements AcvTemplateManageService {

    @Autowired
    AcvTemplateManageMapper acvTemplateManageMapper;

    @Override
    public AjaxResult downloadTemplate() {
        try {
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet("考核项模板");
            //新增一行
            HSSFRow row = sheet.createRow(0);
            HSSFCellStyle txtStyle1 = wb.createCellStyle();
            HSSFFont txtFont = wb.createFont();
            txtFont.setFontName("宋体");
            txtFont.setFontHeightInPoints((short) 9);
            txtFont.setBold(true);
            txtStyle1.setFont(txtFont);
            txtStyle1.setWrapText(true);
            txtStyle1.setAlignment(HorizontalAlignment.CENTER);
            txtStyle1.setVerticalAlignment(VerticalAlignment.CENTER);
            txtStyle1.setBorderBottom(BorderStyle.THIN);
            txtStyle1.setBorderLeft(BorderStyle.THIN);
            txtStyle1.setBorderTop(BorderStyle.THIN);
            txtStyle1.setBorderRight(BorderStyle.THIN);
            row.createCell(0).setCellValue("部门");
            row.createCell(1).setCellValue("岗位");
            row.createCell(2).setCellValue("考核指标");
            row.createCell(3).setCellValue("指标描述/计算公式");
            row.createCell(4).setCellValue("达成目标");
            row.createCell(5).setCellValue("评分标准");
            row.createCell(6).setCellValue("指标配分(满分100分)");
            row.setHeightInPoints(50);
            for (Cell cell : row) {
                cell.setCellStyle(txtStyle1);
            }

            HSSFCellStyle txtStyle = wb.createCellStyle();
            HSSFFont txtFont1 = wb.createFont();
            txtFont1.setFontName("宋体");
            txtFont1.setFontHeightInPoints((short) 9);
            txtStyle.setFont(txtFont1);
            txtStyle.setWrapText(true);
            txtStyle.setAlignment(HorizontalAlignment.CENTER);
            txtStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            txtStyle.setBorderBottom(BorderStyle.THIN);
            txtStyle.setBorderLeft(BorderStyle.THIN);
            txtStyle.setBorderTop(BorderStyle.THIN);
            txtStyle.setBorderRight(BorderStyle.THIN);




            for (int i = 1; i < 11; i++) {
                row = sheet.createRow(i);
                row.createCell(0);
                row.createCell(1);
                row.createCell(2);
                row.createCell(3);
                row.createCell(4);
                row.createCell(5);
                row.createCell(6);
                row.setHeightInPoints(40);
                for (Cell cell : row) {
                    cell.setCellStyle(txtStyle);
                }
            }

            sheet.setColumnWidth(0, 42 * 256);
            sheet.setColumnWidth(1, 42 * 256);
            sheet.setColumnWidth(2, 35 * 256);
            sheet.setColumnWidth(3, 42 * 256);
            sheet.setColumnWidth(4, 32 * 256);
            sheet.setColumnWidth(5, 40 * 256);
            sheet.setColumnWidth(6, 20 * 256);

            String filePath = Global.getDownloadPath() + "考核项模板.xls";
            FileOutputStream fileOut = new FileOutputStream(filePath);
            wb.write(fileOut);
            fileOut.close();
            return AjaxResult.success(0);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("失败");
        }
    }

    @Override
    public List<Map<String, Object>> findJobList() {
        return acvTemplateManageMapper.findJobList();
    }

    @Override
    public int insertTemplateList(int option, String jobName, String jobId, List<Map<String, Object>> templateList,String dept) {
        if(option == 0){
            //新增
            String jobId_I = StringUtil.getUUID();
            acvTemplateManageMapper.insertJob(jobId_I,jobName,dept);
            acvTemplateManageMapper.insertTemplateData(templateList,jobId_I);
            templateList.clear();
        }else{
            //更新
            acvTemplateManageMapper.updateTemplateUsed(jobId);
            acvTemplateManageMapper.insertTemplateData(templateList,jobId);
            templateList.clear();
        }
        return 1;
    }

    @Override
    public List<Map<String, Object>> findTemplateList(String jobId) {
        return acvTemplateManageMapper.findTemplateList(jobId);
    }

    @Override
    public int findJobCount(String jobName,String dept) {
        return acvTemplateManageMapper.findJobCount(jobName,dept);
    }
}
